package com.code.day16.BaoZiPu;

public class BaoZiPu {
    private int count;
    private boolean flag;

    public BaoZiPu() {
    }

    public BaoZiPu(boolean flag, int count) {
        this.flag = flag;
        this.count = count;
    }

    /**
     * 消费包子
     *
     * @return
     */
    public synchronized void getCount() {

        while (this.flag == false) {
            try {
                this.wait();
                System.out.println("没有包子了");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        //有包子
        System.out.println("消费了第..." + count + "个包子");

        this.flag = false;
        //唤醒生产线程
        this.notifyAll();
    }


    /**
     * 生产包子
     */
    public synchronized void setCount() {

        //判断是否有包子 如果有则生产线程等待
        while (this.flag == true) {
            try {
                this.wait();
                System.out.println("有包子了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //如果没有 则生产包子 并且唤醒消费线程

        count++;
        System.out.println("生产了第..." + count + "个包子");
        //修改为True
        this.flag = true;
        //唤醒线程
        this.notifyAll();

    }


    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
